package com.ice.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ice.mapper.ArticleMapper;
import com.ice.pojo.Article;
import com.ice.pojo.PageBean;
import com.ice.service.ArticleService;
import com.ice.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    // 添加文章
    @Override
    public void add(Article article) {
        //设置时间
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        //设置用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);

        articleMapper.add(article);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        //1 创建pageBean 对象
        PageBean<Article> pb = new PageBean<>();
        // 2,开启分类查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);
        //3 ,调用mapper
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<Article> as = articleMapper.list(userId,categoryId,state);
        Page<Article> p = (Page<Article>) as;
        // 把数据填充到PageBean对象里面
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    //获取文章详细信息
    @Override
    public Article select(Integer id) {
        Article article = articleMapper.select(id);
        return article;

    }


    // 更新文章内容下
    @Override
    public void update(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.update(article);
    }

    // s删除文章

    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }
}

































